package com.scpii.universal.dispatch.tools;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.http.client.ClientProtocolException;

import com.scpii.universal.concurrent.Callback;
import com.scpii.universal.exception.DownloadException;
import com.scpii.universal.io.FileUtil;
import com.scpii.universal.io.img.Resize;

/**
 * 图片资源下载处理接口
 * 
 * @author chenxin
 * 
 */
public abstract class PicDownloadHandler {
	/**
	 * 下载图片资源
	 * 
	 * @param picFileDir
	 * @param fileName
	 * @param url
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public abstract void downloadPic(String picFileDir, String fileName,
			String url) throws ClientProtocolException, IOException,
			DownloadException, Exception;

	/**
	 * 保存原始大小图片
	 * 
	 * @param filePath
	 * @param resData
	 * @throws IOException
	 */
	protected void saveNomarlSizePic(String filePath, byte[] resData,
			Callback callback, Map<Object, Object> data) throws IOException,
			Exception {
		FileUtil.savefile(filePath, resData, callback, data);
	}

	/**
	 * 保存双倍大小图片
	 * 
	 * @param filePath
	 * @throws IOException
	 */
	protected void saveDoubleSizePic(String filePathTarget, String filePathSrc)
			throws IOException {
		File file = new File(filePathTarget);
		if (!file.exists()) {
			file.createNewFile();
		}
		BufferedImage bufferedImageSrc = ImageIO.read(new FileInputStream(
				filePathSrc));
		int width = bufferedImageSrc.getWidth();
		int height = bufferedImageSrc.getHeight();
		BufferedImage bufferedImageDes = Resize.rize(bufferedImageSrc,
				width * 2, height * 2);
		FileOutputStream out = new FileOutputStream(filePathTarget);
		ImageIO.write(bufferedImageDes, "png", out);
		out.close();
	}

	/**
	 * 保存半倍大小图片
	 * 
	 * @param filePath
	 * @throws IOException
	 */
	protected void saveHalfSizePic(String filePathTarget, String filePathSrc)
			throws IOException {
		File file = new File(filePathTarget);
		if (!file.exists()) {
			file.createNewFile();
		}
		BufferedImage bufferedImageSrc = ImageIO.read(new FileInputStream(
				filePathSrc));
		int width = bufferedImageSrc.getWidth();
		int height = bufferedImageSrc.getHeight();
		BufferedImage bufferedImageDes = Resize.rize(bufferedImageSrc,
				width / 2, height / 2);
		FileOutputStream out = new FileOutputStream(filePathTarget);
		ImageIO.write(bufferedImageDes, "png", out);
		out.close();
	}

	/**
	 * 修改时间：2013-1-6 修改人：左柳波
	 * 
	 * 保存特定大小的图片
	 * 
	 * @param filePath
	 * @throws IOException
	 */
	protected void saveSpecificSizePic(String filePathTarget,
			String filePathSrc, int width, int height) throws IOException {
		File file = new File(filePathTarget);
		if (!file.exists()) {
			file.createNewFile();
		}
		BufferedImage bufferedImageSrc = ImageIO.read(new FileInputStream(
				filePathSrc));
		BufferedImage bufferedImageDes = Resize.rize(bufferedImageSrc, width,
				height);
		FileOutputStream out = new FileOutputStream(filePathTarget);
		ImageIO.write(bufferedImageDes, "png", out);
		out.close();
	}

}